package com.kzd.btgame.main.money

import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.databinding.ActivityMoneyMallListBinding
import com.kzd.btgame.jetpack.vm.MoneyViewModel
import com.kzd.btgame.main.money.adapter.MoneyTaskMallAdapter
import com.kzd.btgame.main.toMineRecordBillActivity
import com.kzd.btgame.main.toMoneyMallDetActivity
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class MoneyMallListActivity : OwnerViewBindingActivity<ActivityMoneyMallListBinding>() {

    private val mMoneyTaskMallAdapter = MoneyTaskMallAdapter()

    private val mViewModel:MoneyViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityMoneyMallListBinding
        get() = ActivityMoneyMallListBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.tlMoneyTaskMallBar
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {
        mViewBinding.rvMoneyTaskMallBody.adapter = mMoneyTaskMallAdapter.apply {
            setOnItemClickListener { _, _, position ->
                val itemData = getItem(position)
                toMoneyMallDetActivity(itemData.goods_id)
            }
        }

        //兑换记录
        mViewBinding.tvMoneyTaskMallRecord.setOnClickAntiShakeListener {
            toMineRecordBillActivity(index = 2)
        }
    }

    override fun initData() {
        mViewModel.moneyTaskMall()
    }

    override fun onResume() {
        super.onResume()
        mViewModel.moneySign()
    }

    override fun initViewMode() {

        mViewModel.moneySignLiveData.observe(this as LifecycleOwner, { response ->
            mViewBinding.mslMultiStatusLayout.setLoadingStatus(response)
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mViewBinding.tvMoneyTaskMallPoint.text = data.points.toString()
        })

        mViewModel.moneyTaskMallLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mMoneyTaskMallAdapter.setNewInstance(data)
        })
    }
}